package com.hw.paltfrom.service.doncutor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hw.paltfrom.mapper.HwCaseMainMapper;
import com.hw.paltfrom.mapper.HwCommandTaskMapper;
import com.hw.paltfrom.mapper.HwDataOverrunstationOverweightMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.HwCaseMain;
import com.hw.paltfrom.pojo.HwCommandTask;
import com.hw.paltfrom.pojo.HwDataOverrunstationOverweight;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.PageUtils;
import com.hw.paltfrom.vo.DataQueryVo;
import com.hw.paltfrom.vo.FindRoundVo;

@Service
public class DataQueryServer {

	@Autowired
	private HwCommandTaskMapper taskMapper;
	@Autowired
	private HwDataOverrunstationOverweightMapper dataOverrunstationOverweightMapper;

	@Autowired
	private HwCaseMainMapper caseMainMapper;

	public Map<String, Object> query(DataQueryVo vo) {

		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		PageUtils<Map<String, Object>> page = new PageUtils<Map<String, Object>>(pageNum, pageSize);

		// 查询条件

		// 时间范围
		Timestamp StartTime = null;
		Timestamp EndTime = null;

		String cph = vo.getCph();
		String Task_Type = vo.getTask_Type();
		String Flow_Stage = vo.getFlow_Stage();
		String fAreaCode = vo.getfAreaCode();
		String F_FromDepName = vo.getF_FromDepName();
		String F_FromDepCode = vo.getF_FromDepCode();
		// int flag = 0;

		
		if (StringUtils.isBlank(vo.getStartTime())) {
			
			if (StringUtils.isBlank(vo.getEndTime())) {
				// flag = 0;
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		} else {
			if(StringUtils.isNotBlank(vo.getEndTime())){
//			if (vo.getEndTime() != null) {
				StartTime = Timestamp.valueOf(vo.getStartTime());
				EndTime = Timestamp.valueOf(vo.getEndTime());
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		}
		EntityWrapper<HwDataOverrunstationOverweight> weightWrapper = new EntityWrapper<HwDataOverrunstationOverweight>();
		if (StringUtils.isNotBlank(cph)) {
			weightWrapper.like("CPH", cph);
		}
		List<HwDataOverrunstationOverweight> weightList = dataOverrunstationOverweightMapper.selectList(weightWrapper);
		for (HwDataOverrunstationOverweight oow : weightList) {
			// HwCommandTask entity=new HwCommandTask();
			// entity.setfId(oow.getfId());
			// entity.setTaskType(Task_Type);
			// entity.setFlowStage(Flow_Stage);
			// entity.setfAreacode(fAreaCode);
			// entity.setfFromdepcode(F_FromDepCode);
			// HwCommandTask task = taskMapper.selectOne(entity);
			// if(task!=null){
			// result.add(task);
			// }
			EntityWrapper<HwCommandTask> wrapper = new EntityWrapper<HwCommandTask>();
			wrapper.eq("F_Id", oow.getfId());
			if (StringUtils.isNotBlank(Task_Type)) {
				wrapper.eq("Task_Type", Task_Type);
			}
			if (StringUtils.isNotBlank(Flow_Stage)) {
				wrapper.eq("Flow_Stage", Flow_Stage);
			}
			if (StringUtils.isNotBlank(fAreaCode)) {
				wrapper.eq("F_AreaCode", fAreaCode);
			}
			if (StringUtils.isNotBlank(F_FromDepName)) {
				wrapper.eq("F_FromDepName", F_FromDepName);
			}
			if (StringUtils.isNotBlank(F_FromDepCode)) {
				wrapper.eq("F_FromDepCode", F_FromDepCode);
			}

			if (StartTime != null && EndTime != null) {
				wrapper.between("F_CreateTime", StartTime, EndTime);
			}

			List<Map<String, Object>> selectMaps = taskMapper.selectMaps(wrapper);
			if (!selectMaps.isEmpty()) {
				Map<String, Object> map = selectMaps.get(0);
				map.put("cph", oow.getCph());
				result.add(map);
			}
			
		}

		Object info = page.getPageResult(result);

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
	}

	
	public Map<String, Object> queryTask(DataQueryVo vo) {
		
		
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		PageUtils<Map<String, Object>> page = new PageUtils<Map<String, Object>>(pageNum, pageSize);

		// 查询条件

		// 时间范围
		Timestamp StartTime = null;
		Timestamp EndTime = null;

		String Task_Type = vo.getTask_Type();
		String Flow_Stage = vo.getFlow_Stage();
		String fAreaCode = vo.getfAreaCode();
		String F_FromDepName = vo.getF_FromDepName();
		String F_FromDepCode = vo.getF_FromDepCode();
		// int flag = 0;

		
		if (StringUtils.isBlank(vo.getStartTime())) {
			
			if (StringUtils.isBlank(vo.getEndTime())) {
				// flag = 0;
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		} else {
			if(StringUtils.isNotBlank(vo.getEndTime())){
//			if (vo.getEndTime() != null) {
				StartTime = Timestamp.valueOf(vo.getStartTime());
				EndTime = Timestamp.valueOf(vo.getEndTime());
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		}
		
		
		EntityWrapper<HwCommandTask> wrapper = new EntityWrapper<HwCommandTask>();
		if (StringUtils.isNotBlank(Task_Type)) {
			wrapper.eq("Task_Type", Task_Type);
		}
		if (StringUtils.isNotBlank(Flow_Stage)) {
			wrapper.eq("Flow_Stage", Flow_Stage);
		}
		if (StringUtils.isNotBlank(fAreaCode)) {
			wrapper.eq("F_AreaCode", fAreaCode);
		}
		if (StringUtils.isNotBlank(F_FromDepName)) {
			wrapper.eq("F_FromDepName", F_FromDepName);
		}
		if (StringUtils.isNotBlank(F_FromDepCode)) {
			wrapper.eq("F_FromDepCode", F_FromDepCode);
		}

		if (StartTime != null && EndTime != null) {
			wrapper.between("F_CreateTime", StartTime, EndTime);
		}

		List<Map<String, Object>> selectMaps = taskMapper.selectMaps(wrapper);
		
		Object info = page.getPageResult(selectMaps);

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, info);
	}
	
	
	
	public Map<String, Object> findRound(FindRoundVo vo) {
		String fid = vo.getFid();
		String taskid = vo.getTaskid();
		String caseid = vo.getCaseid();

		HashMap<String, Object> result = new HashMap<>();
		if (StringUtils.isNotBlank(fid)) {
			result.put("fid", fid);
			HashMap<String, Object> wapper = new HashMap<String, Object>();
			wapper.put("F_Id", fid);
			List<HwCommandTask> task = taskMapper.selectByMap(wapper);
			if (task != null && !task.isEmpty()) {
				result.put("taskid", task.get(0).getTaskId());
			}
			wapper.clear();
			wapper.put("F_OverrunID", fid);
			List<HwCaseMain> caseMain = caseMainMapper.selectByMap(wapper);
			if (caseMain != null && !caseMain.isEmpty()) {
				result.put("caseid", caseMain.get(0).getfCaseid());
			}
			return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
		}
		if (StringUtils.isNotBlank(taskid)) {
			result.put("taskid", taskid);
			HwCommandTask task = taskMapper.selectById(taskid);
			result.put("fid", task.getfId());
			
			HashMap<String, Object> wapper = new HashMap<String, Object>();
			wapper.put("F_OverrunID", task.getfId());
			List<HwCaseMain> caseMain = caseMainMapper.selectByMap(wapper);
			if (caseMain != null && !caseMain.isEmpty()) {
				result.put("caseid", caseMain.get(0).getfCaseid());
			}
			return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
		}
		if (StringUtils.isNotBlank(caseid)) {
			result.put("caseid", caseid);
			HwCaseMain caseMain = caseMainMapper.selectById(caseid);
			result.put("fid", caseMain.getfOverrunid());
			
			HashMap<String, Object> wapper = new HashMap<String, Object>();
			wapper.put("F_Id", caseMain.getfOverrunid());
			List<HwCommandTask> task = taskMapper.selectByMap(wapper);
			if (task != null && !task.isEmpty()) {
				result.put("taskid", task.get(0).getTaskId());
			}
			return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
		}

		return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
	}
}
